//
//  ViewController.m
//  获取设置的参数
//
//  Created by Clement_Gu on 16/3/16.
//  Copyright © 2016年 小白. All rights reserved.
//

#import "ViewController.h"
#import "ViewController2.h"
@interface ViewController ()
@property(nonatomic,strong)UILabel *labelUserName;
@property(nonatomic,strong)UILabel *labelPassWord;
@property(nonatomic,strong)UILabel *labelProtocol;
@property(nonatomic,strong)UILabel *labelWrap;
@property(nonatomic,strong)UILabel *labelWrapSpeed;
@property(nonatomic,strong)UILabel *labelFavoriteTea;
@property(nonatomic,strong)UILabel *labelFavoriteCandy;
@property(nonatomic,strong)UILabel *labelFavoriteGame;
@property(nonatomic,strong)UILabel *labelFavoriteExcuse;
@property(nonatomic,strong)UILabel *labelFavoriteSin;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //创建显示界面
    self.labelUserName = [self buildLabel:CGRectMake(120, 40, 150, 30) Title:nil];
    self.labelPassWord = [self buildLabel:CGRectMake(120, 80, 150, 30) Title:nil];
    self.labelProtocol = [self buildLabel:CGRectMake(120, 120, 150, 30) Title:nil];
    self.labelWrap = [self buildLabel:CGRectMake(120, 160, 150, 30) Title:nil];
    self.labelWrapSpeed = [self buildLabel:CGRectMake(120, 200, 150, 30) Title:nil];
    self.labelFavoriteTea = [self buildLabel:CGRectMake(120, 240, 150, 30) Title:nil];
    self.labelFavoriteCandy = [self buildLabel:CGRectMake(120, 280, 150, 30) Title:nil];
    self.labelFavoriteGame = [self buildLabel:CGRectMake(120, 320, 150, 30) Title:nil];
    self.labelFavoriteExcuse = [self buildLabel:CGRectMake(120, 360, 150, 30) Title:nil];
    self.labelFavoriteSin = [self buildLabel:CGRectMake(120, 400, 150, 30) Title:nil];
    [self buildLabel:CGRectMake(10, 40, 110, 30) Title:@"用户名"];
    [self buildLabel:CGRectMake(10, 80, 110, 30) Title:@"密码"];
    [self buildLabel:CGRectMake(10, 120, 110, 30) Title:@"传输协议"];
    [self buildLabel:CGRectMake(10, 160, 110, 30) Title:@"滚动"];
    [self buildLabel:CGRectMake(10, 200, 110, 30) Title:@"滚动速度"];
    [self buildLabel:CGRectMake(10, 240, 110, 30) Title:@"最喜欢的茶"];
    [self buildLabel:CGRectMake(10, 280, 110, 30) Title:@"最喜欢的糖果"];
    [self buildLabel:CGRectMake(10, 320, 110, 30) Title:@"最喜欢的游戏"];
    [self buildLabel:CGRectMake(10, 360, 110, 30) Title:@"最喜欢的？"];
    [self buildLabel:CGRectMake(10, 400, 110, 30) Title:@"最喜欢的标签"];
    //添加跳转的button
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(250, 450, 50, 40)];
    [btn setTitle:@"跳转" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(present) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    //1程序载入时更新数据
    [self updateInfo];
    //更新前台的通知 更新界面信息
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateInfo) name:UIApplicationWillEnterForegroundNotification object:nil];
    
    
}
//跳转事件
-(void)present
{
    ViewController2 *vc = [[ViewController2 alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    
    [self presentViewController:nav animated:YES completion:nil];

}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    //3第二个页面回来时候更新下信息
    [self updateInfo];
    
}
//释放通知
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
}

//创建Label
-(UILabel *)buildLabel:(CGRect)frame Title:(NSString *)title
{
    UILabel *label = [[UILabel alloc]initWithFrame:frame];
    label.text = title;
    [self.view addSubview:label];
    return label;
}
     
//显示settings的信息
-(void)updateInfo
{
    //从NSUserDefault 中使用指定的key将信息读取处理
    NSUserDefaults *info = [NSUserDefaults standardUserDefaults];
    self.labelUserName.text = [info valueForKey:@"username"];
    self.labelPassWord.text = [info valueForKey:@"password"];
    self.labelProtocol.text = [info valueForKey:@"protocol"];
    self.labelWrap.text = [info boolForKey:@"wrap"]?@"开":@"关";
    self.labelWrapSpeed.text = [NSString stringWithFormat:@"%f",[info floatForKey:@"wrapSpeed"]];
    self.labelFavoriteTea.text = [info valueForKey:@"favoriteTea"];
    self.labelFavoriteCandy.text = [info valueForKey:@"favoriteCandy"];
    self.labelFavoriteGame.text = [info valueForKey:@"favoriteGame"];
    self.labelFavoriteExcuse.text = [info valueForKey:@"favoriteExcuse"];
    self.labelFavoriteSin.text = [info valueForKey:@"favoriteSin"];

}
     
     
     
     
     
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
